import enquireJs from 'enquire.js'

export function isDef(v) {
  return v !== undefined && v !== null
}

/**
 * Remove an item from an array.
 */
export function remove(arr, item) {
  if (arr.length) {
    const index = arr.indexOf(item)
    if (index > -1) {
      return arr.splice(index, 1)
    }
  }
}

export function isRegExp(v) {
  return _toString.call(v) === '[object RegExp]'
}

export function enquireScreen(call) {
  const handler = {
    match: function () {
      call && call(true)
    },
    unmatch: function () {
      call && call(false)
    }
  }
  enquireJs.register('only screen and (max-width: 767.99px)', handler)
}

export function debounce(fn, delay) {
  // 记录上一次的延时器
  var timer = null;
  var delay2 = delay || 200;
  return function() {
      var args = arguments;
      var that = this;
      // 清除上一次延时器
      clearTimeout(timer)
      timer = setTimeout(function() {
          fn.apply(that, args)
      }, delay2);
  }
}

export function getSearchCriteria(data, mode = 'like') {
  let tempData = {
    search: [],
    searchFields: []
  }
  for (let res in data) {
    if (data[res] !== '') {
      tempData.search.push(`${res}:${data[res]}`)
      if(res.indexOf('id')!=-1){
        tempData.searchFields.push(`${res}:=`)  
      }else{
        tempData.searchFields.push(`${res}:${mode}`)
      }
    }
  }
  let info ={
    searchFields: tempData.searchFields.join(';'),
    search: tempData.search.join(';')
  }
  if(!info.searchFields && !info.search){
    info = {}
  }
  return info
}


const _toString = Object.prototype.toString
